﻿using System;
using System.Collections.Generic;
using System.Linq;
using Missbot.Common;
using Missbot.Service;
using Dolany.UtilityTool;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;

namespace Missbot.AIModule.Pet
{
    /// <summary>
    /// 宠物记录
    /// </summary>
    public class PetRecord : DbBaseEntity
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserID { get; set; }

        /// <summary>
        /// 宠物种族
        /// </summary>
        public string PetNo { get; set; }

        /// <summary>
        /// 宠物名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 宠物等级
        /// </summary>
        public int Level { get; set; }

        /// <summary>
        /// 当前经验值
        /// </summary>
        public int Exp { get; set; }

        /// <summary>
        /// 头像文件ID
        /// </summary>
        public string FileID { get; set; }

        /// <summary>
        /// 食性
        /// </summary>
        public string Attribute { get; set; }

        /// <summary>
        /// 剩余未分配技能点
        /// </summary>
        public int RemainSkillPoints { get; set; }

        /// <summary>
        /// 技能列表
        /// </summary>
        public Dictionary<string, int> Skills { get; set; } = new();

        /// <summary>
        /// 上次喂食时间
        /// </summary>
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime? LastFeedTime { get; set; }
    }

    public static class PetRecordDA
    {
        /// <summary>
        /// 获取宠物记录
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public static PetRecord Get(string UserID)
        {
            var pet = MongoService<PetRecord>.GetOnly(p => p.UserID == UserID);
            if (pet != null)
            {
                return pet;
            }

            pet = new PetRecord
            {
                UserID = UserID,
                PetNo = "Neptune",
                Name = "涅普",
                Level = 1,
                FileID = Configger<AIConfigBase>.AIConfig.Pet.DefaultPetAvatar
            };
            MongoService<PetRecord>.Insert(pet);

            return pet;
        }

        /// <summary>
        /// 更新宠物
        /// </summary>
        public static void Update(this PetRecord entity)
        {
            MongoService<PetRecord>.Update(entity);
        }

        /// <summary>
        /// 宠物等级分析
        /// </summary>
        /// <returns></returns>
        public static Dictionary<int, int> LevelAnalyze()
        {
            var pets = MongoService<PetRecord>.Get();
            return pets.GroupBy(p => p.Level)
                       .ToDictionary(p => p.Key, p => p.Count())
                       .OrderByDescending(p => p.Key)
                       .ToDictionary(p => p.Key, p => p.Value);
        }

        /// <summary>
        /// 宠物等级排名
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public static IEnumerable<PetRecord> LevelTop(int count)
        {
            var sort = Builders<PetRecord>.Sort.Descending(p => p.Level).Descending(p => p.Exp);
            return MongoService<PetRecord>.GetCollection().Find(p => true).Sort(sort).Limit(count).ToList();
        }

        /// <summary>
        /// 技能重置
        /// </summary>
        public static void SkillReset(this PetRecord entity)
        {
            if (entity.Skills.IsNullOrEmpty())
            {
                return;
            }

            var sum = 0;

            for (var i = 0; i < entity.Skills.Count; i++)
            {
                var key = entity.Skills.Keys.ElementAt(i);

                sum += entity.Skills[key] - 1;
                entity.Skills[key] = 1;
            }

            entity.RemainSkillPoints += sum;
        }
    }
}